﻿package kaze.net.remoting.application 
{
    import kaze.logging.logger;
    import kaze.net.remoting.RemotingProtectorListener;
    import kaze.vo.ApplicationVO;
    
    import ninjas.ninpo.process.menu.InitMenu;
    import ninjas.ninpo.process.states.InitState;
    
    import vegas.config.Config;
    import vegas.events.RemotingEvent;
    import vegas.net.remoting.RemotingService;
    import vegas.vo.NetServerInfoVO;
    
    /**
     * Invoked when a RemotingService receive a result from the server during the initialize process of the application
     */
    public class Initialize extends RemotingProtectorListener 
    {
        /**
         * Creates a new Initialize instance.
         * @param service The RemotingService reference of this listener.
         */
        public function Initialize( service:RemotingService = null )
        {
            super( service ) ;
        }
        
        /**
         * The config reference.
         */
        public var config:Config ;
        
        /**
         * initMenu reference
         */
         public var initMenu:InitMenu;
         
        /**
         * initState reference
         */
        public var initState:InitState;
        
        /**
         * Invoked when the service notify a success.
         */
        public override function result( e:RemotingEvent ):void
        {
            super.result(e) ;
            var result:* = e.result ;
            if ( result is ApplicationVO  ) 
            {
                var app:ApplicationVO = result as ApplicationVO ;
                
                logger.info( this + " result success : " + app ) ;
                
                if ( config && app.defaultID != null )
                {
                    config.defaultID = app.defaultID ;
                }
                
                if ( initState != null )
                {
                    initState.run(app.states) ; 
                }
                if ( initMenu != null )
                {
                    initMenu.datas = app.menus ;
                }
            }
            else if ( result is NetServerInfoVO )
            {
                //
            }
            else 
            {
                logger.warn( this + " initialize application failed with an unknow result object : " + result ) ;
            }
        }
    }
}
